const app = getApp();
const api = app.api;
const wxutil = app.wxutil;

Page({
  data: {
    imageFiles: [], // 图片文件列表
    name: '',
    school: '',
    time: '',
    device_name: '',
    area: '',
    orderId: '',
    deviceInfo: {},
    institute: '',
    phoneNum: '',
    selectedDate: '',
    selectedCreateDate:'',
    rentTemplateId: "07iy6kdmWhjxyfzE9c1gkHCJ9qEwVRY9CjY-pOXgTl0",
    endTimeTemplateId: "ka-X7kkTs8G-MDSWABkGPO1lUZVVtmpRtov226FNgy4"
  },

  onLoad(options) {
    const orderId = options.orderId;
    this.setData({
      deviceInfo: wx.getStorageSync("deviceInfo"),
    });
    if (orderId != null) {
      this.setData({
        orderId: orderId,
      });
    }
  },

  /**
   * 设置内容
   */

  setName(event) {
    this.setData({
      name: event.detail.value,
    });
  },
  setSchool(event) {
    this.setData({
      school: event.detail.value,
    });
  },

  setNumber(event) {
    this.setData({
      time: event.detail.count,
    });
  },

  setDeviceName(event) {
    this.setData({
      device_name: event.detail.value,
    });
  },

  setArea(event) {
    this.setData({
      area: event.detail.value,
    });
  },
  setInstitute(event) {
    this.setData({
      institute: event.detail.value,
    });
  },
  setPhone(event) {
    this.setData({
      phoneNum: event.detail.value,
    });
  },
  onDateChange: function (e) {
    this.setData({
      selectedDate: e.detail.value
    });
  },
  onCreateDateChange: function (e) {
    this.setData({
      selectedCreateDate: e.detail.value
    });
  },

  /**
   * 选择图片
   */
  onChangeImage(event) {
    const files = event.detail.all;
    let imageFiles = [];
    for (let i = 0; i < files.length; i++) {
      imageFiles.push(files[i].url);
    }
    this.setData({
      imageFiles: imageFiles,
    });
  },

  /**
   * 多图上传
   */
  sendImages(imageFiles) {
    const url = api.mallauthAPI + "images/";
    return Promise.all(
      imageFiles.map((imageFile) => {
        return new Promise(function (resolve, reject) {
          wxutil.file
            .upload({
              url: url,
              fileKey: "file",
              filePath: imageFile,
            })
            .then((res) => {
              const data = JSON.parse(res.data);
              if (data.code == 200) {
                resolve(data.data.url);
              }
            })
            .catch((error) => {
              reject(error);
            });
        });
      })
    );
  },


  /**
   * 点击发布
   */
  onSubmitTap() {
    const name = this.data.name;
    const orderId = this.data.orderId;
    const time = this.data.time;
    const device_name = this.data.deviceInfo.device_info.device_name;
    const area = this.data.deviceInfo.area;
    const school = this.data.school;
    const institute = this.data.deviceInfo.device_info.institute_name;
    const phoneNum = this.data.phoneNum;
    const endTime = new Date(this.data.selectedDate);
    const createTime = new Date(this.data.selectedCreateDate)
    if (!wxutil.isNotNull(name)) {
      wx.lin.showMessage({
        type: "error",
        content: "请输入姓名！",
      });
      return;
    }

    // 授权模板消息
    const templateId = this.data.rentTemplateId;
    const endTimeTemplateId = this.data.endTimeTemplateId
    console.log(endTimeTemplateId)
    const that = this;

    wx.requestSubscribeMessage({
      tmplIds: [templateId,endTimeTemplateId],
      complete() {
        // 发布话题
        wxutil.showLoading("发布中...");
        let data = {
          deviceName: device_name,
          device_num: time,
          device_area: area,
          orderId: orderId,
          rentInstitute: school,
          institute_name: institute,
          phoneNum: phoneNum,
          createTime : createTime,
          endTime: endTime

        };
        that.uploadTopic(data);
      },
    });
  },

  /**
   * 上传话题
   */
  uploadTopic(data) {
    const url = api.userAPI + "rent/";

    wxutil.request.post(url, data).then((res) => {
      wx.hideLoading();
      if (res.data.code == 200) {
        wx.lin.showMessage({
          type: "success",
          content: "上传成功",
          success() {
            wxutil.setStorage("refreshTopics", true);
            wx.navigateBack();
          },
        });
      } else {
        wx.lin.showMessage({
          type: "error",
          content: "上传失败",
        });
      }
    });
  },

  onShareAppMessage() {
    return {
      title: "租赁表单",
      path: "/pages/rent-device/index",
    };
  },
});
